package com.maimao.auth.imageCode.username;

import com.maimao.auth.imageCode.AbstractPwdCodeTokenGranter;
import com.maimao.auth.mobilecCode.MobileCodeAuthenticationToken;
import com.maimao.core.constant.AuthKey;
import com.maimao.core.constant.GrantTypeEnum;
import com.maimao.core.utils.RegexUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.authentication.AccountStatusException;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.common.exceptions.InvalidGrantException;
import org.springframework.security.oauth2.provider.*;
import org.springframework.security.oauth2.provider.token.AbstractTokenGranter;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 用户名密码+图形验证码授权方式
 *
 * @author MaoLin Wang
 * @date 2020/12/14 3:15 下午
 */
public class UserNameCodeTokenGranter extends AbstractPwdCodeTokenGranter {

    private static final String GRANT_TYPE = GrantTypeEnum.USERNAME_PWD_CODE.getType();
    private static final int CODE_LENGTH = 5;
    private final AuthenticationManager authenticationManager;

    public UserNameCodeTokenGranter(AuthorizationServerTokenServices tokenServices,
                                    ClientDetailsService clientDetailsService, OAuth2RequestFactory requestFactory, final AuthenticationManager authenticationManager) {
        super(tokenServices, clientDetailsService, requestFactory, GRANT_TYPE);
        this.authenticationManager = authenticationManager;
    }

    @Override
    protected Integer getCodeLength() {
        return CODE_LENGTH;
    }

    @Override
    protected Authentication createAuthenticationToken(String username, String password, String code,String ip) {
        return new UserNameCodeAuthenticationToken(username, password, code,ip);
    }

    @Override
    protected AuthenticationManager authenticationManager() {
        return this.authenticationManager;
    }


}
